package com.lanren.familybookapp.ui.home.presenter;

import com.lanren.common.base.impl.BasePresenter;
import com.lanren.common.utils.StringUtils;
import com.lanren.easyhttp.body.UIProgressResponseCallBack;
import com.lanren.easyhttp.subsciber.IProgressDialog;
import com.lanren.familybookapp.ui.home.interfaces.ICSVImportModel;
import com.lanren.familybookapp.ui.home.model.CSVImportModel;

import java.io.File;

/**
 * @ClassName CSVImportPresenter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/2/13 17:33
 * @Version 1.0
 */
public class CSVImportPresenter extends BasePresenter<ICSVImportModel.ICSVImportView> {
    private CSVImportModel csvImportModel;

    public CSVImportPresenter(CSVImportModel csvImportModel) {
        this.csvImportModel = csvImportModel;
    }

    public void bookImport(int bookId, int bookType, String bookMomber, File file, IProgressDialog mProgressDialog
            , final UIProgressResponseCallBack listener) {

        if (StringUtils.intIsNull(bookType)) {
            mRootView.showMessage("账单来源不能为空");
            return;
        }
        if (file == null) {
            mRootView.showMessage("上传文件不能为空");
            return;
        }
        String[] fileName = file.getName().split("\\.");
        if (!fileName[1].equals("csv")) {
            mRootView.showMessage("上传文件不是csv文件，请选择正确的文件");
            return;
        }
        if (file.getName().toLowerCase().contains("ali") || file.getName().contains("支付宝")) {
            if (bookType != 1) {
                mRootView.showMessage("账单来源不是支付宝账单,请重新选择");
                return;
            }
        } else if (file.getName().toLowerCase().contains("wechat") || file.getName().contains("微信")) {
            if (bookType != 2) {
                mRootView.showMessage("账单来源不是微信账单，请重新选择");
                return;
            }
        } else {
            mRootView.showMessage("账单来源异常，请仔细查看选择文件");
            return;
        }

        csvImportModel.bookImport(bookId, bookType, bookMomber, file, mProgressDialog, listener, new ICSVImportModel.CSVImportModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(String msg) {
                mRootView.onSuccess(msg);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (csvImportModel != null) {
            csvImportModel.clearDisposable();
            csvImportModel = null;
        }

    }
}
